package de.jamm.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class MapPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BufferedImage imgT = null;
	private BufferedImage stoneWhite = null;
	private BufferedImage stoneBlack = null;
	private BufferedImage stoneGreen = null;
	private BufferedImage stoneWhiteH = null;
	private BufferedImage stoneBlackH = null;
	private BufferedImage stoneMoveWhite = null;
	private BufferedImage stoneMoveBlack = null;

	
	public MapPanel() {
		// TODO Auto-generated constructor stub
		
		new MapField(57,55,0);
		new MapField(285,56,1);
		new MapField(510,55,2);
		new MapField(511,280,3);
		new MapField(509,507,4);
		new MapField(283,508,5);
		new MapField(57,507,6);
		new MapField(57,282,7);
		new MapField(121,116,8);
		new MapField(283,116,9);
		new MapField(449,115,10);
		new MapField(450,282,11);
		new MapField(448,445,12);
		new MapField(284,444,13);
		new MapField(120,443,14);
		new MapField(119,281,15);
		new MapField(192,189,16);
		new MapField(284,189,17);
		new MapField(376,190,18);
		new MapField(375,279,19);
		new MapField(374,374,20);
		new MapField(284,373,21);
		new MapField(191,373,22);
		new MapField(191,281,23);

//		new MapField(40, 40);
//		new MapField(40, 40);
//		new MapField(40, 40);
//		new MapField(40, 40);
		
		
		super.setBackground(new Color(0,0,0,0));
		URL url = getClass().getResource("images/map.png");
		
		try {
			imgT = ImageIO.read(url);
			} catch (IOException ioex){
		}
		url = getClass().getResource("images/stone_b.png");
		
		try {
			stoneBlack = ImageIO.read(url);
			} catch (IOException ioex){
		}
		url = getClass().getResource("images/stone_w.png");
		
		try {
			stoneWhite = ImageIO.read(url);
			} catch (IOException ioex){
		}
		
		url = getClass().getResource("images/stone_b_h.png");
		
		try {
			stoneBlackH = ImageIO.read(url);
			} catch (IOException ioex){
		}
		url = getClass().getResource("images/stone_w_h.png");
		
		try {
			stoneWhiteH = ImageIO.read(url);
			} catch (IOException ioex){
		}
		
		url = getClass().getResource("images/stone_g.png");
		
		try {
			stoneGreen = ImageIO.read(url);
			} catch (IOException ioex){
		}
		
		url = getClass().getResource("images/stone_b_m.png");
		
		try {
			stoneMoveBlack = ImageIO.read(url);
			} catch (IOException ioex){
		}
		url = getClass().getResource("images/stone_w_m.png");
		
		try {
			stoneMoveWhite = ImageIO.read(url);
			} catch (IOException ioex){
		}
		
		this.addMouseMotionListener(new MouseMotionListener() {
			
			@Override
			public void mouseMoved(MouseEvent arg0) {
				// TODO Auto-generated method stub
				for(MapField temp:MapField.getAllFields()){
					Point c = getCenter();
					Point p = new Point((int)(arg0.getX()-c.getX()),(int)(arg0.getY()-c.getY()));
					if(temp.contains(p)){
						temp.setSelected(true);
					}else{
						temp.setSelected(false);
					}
				}
				GuiController.getInstance().repaintGui();
			}
			
			@Override
			public void mouseDragged(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});
		this.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				Point a = getCenter();
				Point b = new Point((int)(arg0.getX()-a.getX()),(int)(arg0.getY()-a.getY()));

				Stone choosen = Stone.getStoneByPoint(b);
				if(choosen != null){
					choosen.setSelected(true);
				}
				
				MapField mf = MapField.getSelected();
				Stone st = Stone.getSelected();
				
				if(mf != null && st != null){
					st.sendMove(mf,50);
				}
				GuiController.getInstance().repaintGui();
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}	
	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Point local = getCenter();
		g.translate((int)local.getX(), (int)local.getY());
		g.drawImage(imgT,0,0,null);
		drawStones(g);
	}
	private void drawStones(Graphics g) {
		// TODO Auto-generated method stub
		for(Stone temp:Stone.getAllStones()){
			if(!temp.isDelete()){
				if(temp.getType() == 1){
					if(!temp.isMove()){
						if(temp.isSelected()){
							g.drawImage(stoneWhiteH, temp.getX(), temp.getY(),null);
						}else{
							g.drawImage(stoneWhite, temp.getX(), temp.getY(),null);
						}
					}
				}else{
					if(!temp.isMove()){
						if(temp.isSelected()){
							g.drawImage(stoneBlackH, temp.getX(), temp.getY(),null);				
						}else{
							g.drawImage(stoneBlack, temp.getX(), temp.getY(),null);
						}
					}
				}
			}
		}
		
		for(MapField temp:MapField.getAllFields()){
			
			if(temp.isSelected()){
				g.setColor(Color.GREEN);
				Polygon select = temp.getPolygon();
				g.drawImage(stoneGreen,(int)select.getBounds().getX(),(int)select.getBounds().getY(),null);

			}else{
				g.setColor(Color.BLACK);
			}			
			g.setColor(Color.BLACK);
		}
		
		for(Stone temp:Stone.getAllStones()){
			if(temp.isMove()){
				if(temp.getType() == 1)
					g.drawImage(stoneMoveWhite, temp.getX(), temp.getY(),null);
				else
					g.drawImage(stoneMoveBlack, temp.getX(), temp.getY(),null);
			}
		}
	}
	public Point getCenter(){		
		return new Point((this.getWidth()/2-imgT.getWidth()/2),(this.getHeight()/2-imgT.getHeight()/2));
	}
}